package two.chapter_1.c1_2;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

import java.util.Arrays;

public class Ex1_2_10 {


    public static void main(String[] args) {
        int N = 100;
        int max = 20;
        VisualCounter visualCounter = new VisualCounter(N, max);

        for (int i = 0; i < N; i++) {
            if (StdRandom.bernoulli(0.5)) {
                visualCounter.increment();
            } else {
                visualCounter.decrement();
            }
        }
    }

    private static class VisualCounter {

        private int count;
        private int N;
        private int max;
        private int modifyCount;

        public VisualCounter(int N, int max) {
            this.N = N;
            this.max = max;
            StdDraw.setXscale(0, N);
            StdDraw.setYscale(-max, max);
            StdDraw.setPenRadius(0.01);
        }

        public void increment() {
            if (count + 1 <= max && modifyCount < N) {
                count++;
                modifyCount++;
                draw();
            }
        }

        public void decrement() {
            if (modifyCount < N && Math.abs(count-1)<max) {
                modifyCount++;
                count--;
                draw();
            }
        }


        private void draw() {

            StdDraw.point(modifyCount, count);
        }
    }


}
